package cn.edu.sgu.www.mhxysy.service.jiejing.impl;

import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.jiejing.JiejingCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.jiejing.JiejingCategory;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.jiejing.JiejingCategoryMapper;
import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.jiejing.JiejingCategoryService;
import cn.edu.sgu.www.mhxysy.util.ImageUpdater;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.UploadUtils;
import cn.edu.sgu.www.mhxysy.util.WrapperUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class JiejingCategoryServiceImpl implements JiejingCategoryService {

    private final UploadUtils uploadUtils;
    private final ImageUpdater imageUpdater;
    private final JiejingCategoryMapper jiejingCategoryMapper;

    @Autowired
    public JiejingCategoryServiceImpl(
            UploadUtils uploadUtils,
            ImageUpdater imageUpdater,
            JiejingCategoryMapper jiejingCategoryMapper) {
        this.uploadUtils = uploadUtils;
        this.imageUpdater = imageUpdater;
        this.jiejingCategoryMapper = jiejingCategoryMapper;
    }

    @Override
    public String upload(MultipartFile file) throws IOException {
        String directory = CustomDirectories.DIRECTORY_JIEJING;

        return uploadUtils.upload(file, directory);
    }

    @Override
    public void insert(JiejingCategoryDTO jiejingCategoryDTO) {
        JiejingCategory jiejingCategory = toEntity(jiejingCategoryDTO);

        jiejingCategory.setId(null);

        jiejingCategoryMapper.insert(jiejingCategory);
    }

    @Override
    public void updateById(JiejingCategoryDTO jiejingCategoryDTO) {
        ObjectUtils.requireNonNull(jiejingCategoryDTO.getId(), CustomMessages.ID_NOT_NULL);

        JiejingCategory jiejingCategory = toEntity(jiejingCategoryDTO);

        jiejingCategoryMapper.updateById(jiejingCategory);
    }

    @Override
    public void updateImageById(Integer id, String image) {
        imageUpdater.updateImageById(image, new Consumer<Object>() {
            @Override
            public void accept(Object o) {
                UpdateWrapper<JiejingCategory> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

                jiejingCategoryMapper.update(updateWrapper.getEntity(), updateWrapper);
            }
        });
    }

    @Override
    public Page<JiejingCategory> selectByPage(Pager<JiejingCategory> pager) {
        Page<JiejingCategory> page = Pager.ofPage(pager);

        return jiejingCategoryMapper.selectPage(page, null);
    }

    private JiejingCategory toEntity(JiejingCategoryDTO jiejingCategoryDTO) {
        Integer id = jiejingCategoryDTO.getId();
        String name = jiejingCategoryDTO.getName();

        QueryWrapper<JiejingCategory> queryWrapper = new QueryWrapper<>();

        queryWrapper.ne(id != null, "id", id);
        queryWrapper.eq("name", name);

        Long count = jiejingCategoryMapper.selectCount(queryWrapper);

        if (count > 0) {
            throw new GlobalException(ResponseCode.CONFLICT, "名称重复，保存失败！");
        }

        JiejingCategory jiejingCategory = new JiejingCategory();

        jiejingCategory.setId(id);
        jiejingCategory.setName(name);
        jiejingCategory.setNote(jiejingCategoryDTO.getNote());
        jiejingCategory.setTexiaoId(jiejingCategoryDTO.getTexiaoId());

        return jiejingCategory;
    }

}